/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.page.PagerResult;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.permit.controller.assembler.PermitAssembler;
import net.siufung.market.base.permit.controller.model.dto.PermitDto;
import net.siufung.market.base.permit.controller.model.dto.PermitSaveDto;
import net.siufung.market.base.permit.controller.model.dto.PermitUpdateDto;
import net.siufung.market.base.permit.controller.model.dto.page.PermitPageDto;
import net.siufung.market.base.permit.controller.model.vo.PermitVo;
import net.siufung.market.base.permit.mapper.IPermitMapper;
import net.siufung.market.base.permit.mapper.entity.Permit;
import net.siufung.market.base.permit.service.IPermitQueryService;
import net.siufung.market.base.permit.service.IPermitService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 系统权限 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-05 22:19:50
 */
@Api(tags = "权限管理")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/permit")
public class PermitController {

    private final IPermitMapper permitMapper;
    private final IPermitService permitService;
    private final IPermitQueryService permitQueryService;

    /**
     * 通过主键获取 系统权限表
     * @param id 主键
     * @return Result<PermitVo>
     * */
    @ApiOperation(value = "通过Id获取系统权限表信息")
    @GetMapping(value = "/id/{id}")
    public Result<PermitVo> getById(
            @PathVariable("id") Integer id) {
        return Result.ok(
            PermitAssembler.from(permitMapper.selectById(id)));
    }

    /**
     * 获取 系统权限表列表
     * @param permitDto 属性
     * @return Result<List<PermitVo>>
     * */
    @ApiOperation(value = "获取系统权限表列表")
    @PostMapping(value = "/list")
    public Result<List<PermitVo>> list(
            @RequestBody PermitDto permitDto) {
        return Result.ok(
            PermitAssembler.fromList(permitMapper.selectList(Wrappers.query(permitDto.to()))));
    }

    /**
     * 获取 系统权限表分页
     * @param permitPageDto 属性
     * @return 查询分页结果
     * */
    @ApiOperation(value = "获取系统权限表分页")
    @PostMapping(value="/page")
    public Result<PagerResult<PermitVo>> getPage(
            @RequestBody PermitPageDto permitPageDto) {
        permitPageDto.validated();
        QueryWrapper<Permit> wrapper = new QueryWrapper<>();
                    wrapper.lambda().orderByDesc(Permit::getId);
        IPage<Permit> permitPage = permitMapper.selectPage(
                new Page<>(permitPageDto.getPageNo(), permitPageDto.getPageSize()), wrapper);
        PagerResult<PermitVo> permitVoPage = new PagerResult<>(
                permitPage.getCurrent(), permitPage.getSize(), permitPage.getTotal());
        permitVoPage.setRecords(
            PermitAssembler.fromList(permitPage.getRecords()));
        return Result.ok(permitVoPage);
    }

    /**
     * 通过主键删除 系统权限表 对象
     * @param id 主键
     * @return 删除结果
     * */
    @ApiOperation(value = "删除系统权限表对象")
    @PostMapping(value="/delete/{id}")
    public Result<Void> deleteById(
        @PathVariable("id") Integer id) {
        if(permitService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键集合批量删除 系统权限表
     * @param ids 主键集合
     * @return 批量删除结果
     * */
    @ApiOperation(value = "批量删除系统权限表对象")
    @PostMapping(value="/deleteBatch")
    public Result<Void> deleteBatch(@RequestBody Integer[] ids) {
        if(permitService.removeByIds(Arrays.asList(ids))){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 创建 系统权限表
     * @param permitSaveDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "创建 系统权限表")
    @PostMapping(value="/create")
    public Result<Void> create(
            @RequestBody PermitSaveDto permitSaveDto) {
        permitSaveDto.validated();
        if(permitService.save(permitSaveDto.to())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 更新 系统权限表
     * @param permitUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统权限表 信息")
    @PostMapping(value="/updateById")
    public Result<Void> updateById(
            @RequestBody PermitUpdateDto permitUpdateDto) {
        permitUpdateDto.validated();
        Permit permitOf = permitService.getById(permitUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(permitUpdateDto.to(), permitOf);
        if(permitService.updateById(permitOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 更新 系统权限表 状态
     * @param id 主键
     * @param deleted 数据状态
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统权限表 状态")
    @PostMapping(value="/updateStatusById/{id}/{deleted}")
    public Result<Void> updateStatusById(
            @PathVariable("id") Integer id,
            @PathVariable("deleted") boolean deleted) {
        if(permitService.updateStatusById(id, DeletedEnum.get(deleted))){
            return Result.ok(ResultOk.STATUS);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 获得权限树
     * @return Result<List<Tree<Integer>>>
     */
    @ApiOperation(value = "获得权限树")
    @PostMapping(value="/getTreeList")
    public Result<List<Tree<Integer>>> getTreeList() {
        return Result.ok(permitQueryService.getTreeList());
    }

}